/*
 * @Author: qin
 * @Date:   2018-08-28
 * +----------------------------------------------------------------------
 * | admin [ 后台管理系统公共配置信息 ]
 * | 用户模块api
 * +----------------------------------------------------------------------
 */
layui.define(['jquery', 'configure', 'configureCom', 'sessionFun'], function (exports) {
    window.$ = window.jQuery = layui.$;
    var configure = layui.configure;
    var configureCom = layui.configureCom;
    var registe = {
        //打开登记弹窗
        layer: function (bdId, singleProjectId, type, by, confirmType) {
            var _by = by || '';
            var url = configure.KCDJ_REGISTER_DETAIL_HTML + '?bdId=' + bdId + '&singleProjectId=' + singleProjectId + '&type=' + type + '&by=' + _by;
            var title = '成果登记';
            var btn = 1;
            if (type !== 'edit') {
                btn = false;
                title = '查看成果详情';
            } else {
                title = '成果登记';
            }
            if(type=='edit'){
                var btns = confirmType == 'confirm'?['保存']:['保存', '完成登记'];
                top.layer.open({
                    type: 2,
                    area: ['900px', '600px'],
                    title: title,
                    content: url,
                    btn: btns,
                    yes: function (index, layero) {
                        var iframeWin = top.window.frames['layui-layer-iframe' + index]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        iframeWin.parWin = window;
                        if (type === 'edit') {
                            window.saveForm = function (data, formList) {
                                //要提交的数据
                                var url = configure.KCDJ_SUBMIT_CHENGGUO_HTTP;
                                var ajaxData = JSON.stringify(data);
                                configureCom.postAjax(url, ajaxData).then(function (data) {
                                    if (data.code === 1) {
                                        if (formList.length > 0) {
                                            var formUrl = configure.KCDJ_CONFIRM_SPECIAL_TYPE_HTTP;
                                            configureCom.postAjax(formUrl, JSON.stringify(formList)).then(function (data) {
                                                configureCom.loadOff();
                                                if (data.code === 1) {
                                                    var msg = '专业列表保存成功';
                                                    top.layer.msg('保存成功');
                                                    try {
                                                        parent.refreshIframe('registeGain');
                                                    } catch (e) {
                                                        var mag = '不需要刷新tab';
                                                    }
                                                    top.layer.close(index);
                                                } else {
                                                    top.layer.msg('列表保存失败');
                                                }
                                            });
                                        } else {
                                            configureCom.loadOff();
                                            top.layer.msg('保存成功');
                                            try {
                                                parent.refreshIframe('registeGain');
                                            } catch (e) {
                                                var mag = '不需要刷新tab';
                                            }
                                            top.layer.close(index);
                                        }
                                    } else {
                                        configureCom.loadOff();
                                        top.layer.msg(data.msg);
                                    }
                                });
                            };
                            $(iframeWin.document).find('.demo1').click();
                        }
                    },
                    btn2: function (index, layero) {
                        var iframeWin = top.window.frames['layui-layer-iframe' + index]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        iframeWin.parWin = window;
                        window.saveForm = function (data, formList) {
                            //要提交的数据
                            var url = configure.KCDJ_SUBMIT_CHENGGUO_HTTP;
                            var ajaxData = JSON.stringify(data);
                            configureCom.postAjax(url, ajaxData).then(function (data) {
                                if (data.code === 1) {
                                    if (formList.length > 0) {
                                        var formUrl = configure.KCDJ_CONFIRM_SPECIAL_TYPE_HTTP;
                                        configureCom.postAjax(formUrl, JSON.stringify(formList)).then(function (data) {
                                            configureCom.loadOff();
                                            if (data.code === 1) {
                                                configureCom.postDataAjax(configure.KCDJ_SUBMIT_REGISTER_HTTP, {
                                                    projBdstId: bdId,
                                                    singleProjectId: singleProjectId
                                                }).then(function (res) {
                                                    if (res.code == 1) {
                                                        top.layer.msg('登记成功');
                                                        try {
                                                            parent.refreshIframe('registeGain');
                                                        } catch (e) {
                                                            var mag = '不需要刷新tab';
                                                        }
                                                        top.layer.close(index);
                                                    } else {
                                                        top.layer.alert(res.msg);
                                                    }
                                                });
                                            } else {
                                                top.layer.msg('列表保存失败');
                                            }
                                        });
                                    } else {
                                        configureCom.postDataAjax(configure.KCDJ_SUBMIT_REGISTER_HTTP, {
                                            projBdstId: bdId,
                                            singleProjectId: singleProjectId
                                        }).then(function (res) {
                                            if (res.code == 1) {
                                                top.layer.msg('登记成功');
                                                try {
                                                    parent.refreshIframe('registeGain');
                                                } catch (e) {
                                                    var mag = '不需要刷新tab';
                                                }
                                                top.layer.close(index);
                                            } else {
                                                top.layer.alert(res.msg);
                                            }
                                        });
                                    }
                                } else {
                                    configureCom.loadOff();
                                    top.layer.msg(data.msg);
                                }
                            });
                        };
                        $(iframeWin.document).find('.demo1').click();
                        return false;
                    }
                });
            }else{
                top.layer.open({
                    type: 2,
                    area: ['900px', '600px'],
                    title: title,
                    content: url
                });
            }

        }
    };
    exports('registe', registe);
});